package com.flute.icrawler.check.formxml;

import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.output.XMLOutputter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.flute.icrawler.check.util.SerializeId;
import com.flute.icrawler.check.util.Util;


public class CrawlerCheckSerivce {
	private static final Logger logger = LoggerFactory.getLogger(CrawlerCheckSerivce.class);
	static CrawlerCheckSerivce service = new CrawlerCheckSerivce();
	
	public static CrawlerCheckSerivce getInstance(){
		return service;
	}
	public void writeXml(Map<String,List<String>> dataMap){
		try{
			logger.info("执行 writeXml dataMap.size= "+dataMap.size());
			Iterator<String> it = dataMap.keySet().iterator();
			while(it.hasNext()){
				String keyUrl = it.next();
				String url = keyUrl;
				logger.info("执行 writeXml keyUrl= "+keyUrl);
				List<String> list = dataMap.get(keyUrl);
				keyUrl = filtrato4Url(keyUrl);
				Element rootElt = new Element("root");  //fileName
				Element urlElt = new Element("url"); 
				urlElt.setAttribute("id",url);
				Element fileNameElt = new Element("fileName"); 
				rootElt.addContent(urlElt);
				rootElt.addContent(fileNameElt);
				fileNameElt.setAttribute("id", keyUrl);
				for(String strurl : list){
					Element hrefElt = new Element("href"); 
					fileNameElt.addContent(hrefElt);
					strurl = filtrato4Url(strurl);
					hrefElt.addContent(strurl);
				}
				OutPutFile(rootElt,keyUrl);
			}
		}catch(Exception e){
			logger.error(" writeXml error :" + e);
		}
		
	}
	private String filtrato4Url(String str){
		if(str!=null&&str.startsWith("http://")){
			str = str.replace("http://", "");
		}
		return str;
	}
	private String checkFilename(String str){
		String fileName = "";
		if(str.indexOf("\\")!=-1){
			str = str.replace("\\", "");
		} if(str.indexOf("/")!=-1){
			str = str.replace("/", "");
		} if(str.indexOf(":")!=-1){
			str = str.replace(":", "");
		} if(str.indexOf("*")!=-1){
			str = str.replace("*", "");
		} if(str.indexOf("?")!=-1){
			str = str.replace("?", "");
		} if(str.indexOf("\"")!=-1){
			str = str.replace("\"", "");
		} if(str.indexOf("<")!=-1){
			str = str.replace("<", "");
		} if(str.indexOf(">")!=-1){
			str = str.replace(">", "");
		} if(str.indexOf("|")!=-1){
			str = str.replace("|", "");
		}
		if(str.indexOf("&layer") != -1){
			str = Util.findTargetInStr("&layer=([0-9]){1}", str) + Util.findTargetInStr("id=(.*)&", str);
		}else{
			str =  Util.findTargetInStr("id=(.*)&", str);
		}
		
		fileName = str + ".xml";
		
		return fileName;
	}
	private void OutPutFile(Element root,String keyUrl){
		
		
		String filename = checkFilename(keyUrl);
		Document document = new Document(root);
		org.jdom.output.Format format = org.jdom.output.Format.getCompactFormat(); 
		format.setEncoding("gbk"); 
		format.setIndent("　 "); //缩进2个空格后换行,空格数自己设 de  
	    XMLOutputter outputter = new XMLOutputter(format);
	    try {
	    	String dir = Util.getRootDir("CrawlTestFiles") + "/" + filename;
			outputter.output(document, new FileOutputStream(dir));
			logger.info(" 已经写入文件 " + filename);
		} catch (Exception e) {
			logger.error(" OutPutFile error " , e);
		} 

	}
	public static void main(String args[]){
		List<String> list = new ArrayList<String>();
		list.add("htmlTest.do?id=993&layer=3");
		list.add("htmlTest.do?id=994&layer=3");
		list.add("htmlTest.do?id=995&layer=3");
		Map<String,List<String>> dataMap = new HashMap<String,List<String>>();
		dataMap.put("http://192.168.1.125:8088/htmlTest/htmlTest.do?id=8589&layer=2", list);
		CrawlerCheckSerivce.getInstance().writeXml(dataMap);
	}

}
